package com.text.controller;

import com.github.pagehelper.PageInfo;
import com.text.entity.Car;
import com.text.entity.Users;
import com.text.service.CarService;
import jakarta.annotation.Resource;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;



/**
 * (Car)表控制层
 *
 * @author makejava
 * @since 2023-06-27 14:58:35
 */
@RestController
@RequestMapping("/admin")
public class CarController {
    /**
     * 服务对象
     */
    @Resource
    private CarService carService;

    @Resource
    private RedisTemplate redisTemplate;

    /**
     * 分页查询
     *
     * @param car 筛选条件
     * @return 查询结果
     */
    @GetMapping("/carList")
    public ResponseEntity<PageInfo<Car>> queryByPage(@RequestBody(required = false) Car car,
                                                       @RequestParam(value = "pageNum",required = false,defaultValue = "1") Integer pageNum,
                                                       @RequestParam(value = "pageSize",required = false,defaultValue = "5") Integer pageSize) {
        PageInfo<Car> carListPage = this.carService.queryByPage(car,pageNum,pageSize);
        return ResponseEntity.ok(carListPage);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/car/{id}")
    public ResponseEntity<Car> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.carService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param car 实体
     * @return 新增结果
     */
    @PostMapping("/carAdd")
    public ResponseEntity<Car> add(@RequestBody Car car) {
        return ResponseEntity.ok(this.carService.insert(car));
    }

    /**
     * 编辑数据
     *
     * @param car 实体
     * @return 编辑结果
     */
    @PutMapping("/carEdit")
    public ResponseEntity<Car> edit(Car car) {
        return ResponseEntity.ok(this.carService.update(car));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping("/carDelete")
    public ResponseEntity<Boolean> deleteById(Integer id) {
        boolean f =this.carService.deleteById(id);
        redisTemplate.delete("car_" + id);
        return ResponseEntity.ok(f);
    }

}

